package zozopo.user;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.comm.Validate;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyPasswordByCode  extends ActionSupport implements Action ,SessionAware{
	private static final Log logger = LogFactory.getLog(LogFactory.class);
	
	private String qq;
	private String code;
	private String password;
	private String comfirmpassword;
	private String valicode;
	
	private Map<Object, Object> session;
	
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getComfirmpassword() {
		return comfirmpassword;
	}
	public void setComfirmpassword(String comfirmpassword) {
		this.comfirmpassword = comfirmpassword;
	}
	public String execute(){
		String forward = SUCCESS;
		
		return forward;
	}
	public void validate() {
		
		// 若debug模式开启，则输出debug 信息
        if (logger.isDebugEnabled()) {
           // logger.debug(getUsername() +" "+getPassword() + " " + getValicode());
        }
        //验证码
        
       // System.out.println(getValicode());
		ActionContext actionContext = ActionContext.getContext(); 
		
		String valicode = (String) actionContext.getSession().get("valicode");
		
		
		if(valicode == null || !valicode.equals(getValicode())){
			addFieldError("errormessage",getText("background.login.valicode.iswrong"));
			return;
		}
		
		
		
		if(!password.equals(comfirmpassword)){
			addFieldError("errormessage","输入两次密码不一致请重新输入");
			return;
		}
		if(password.length() <4){
			addFieldError("errormessage","密码长度不能少于4个字符");
			return;
		}

		if(!Validate.isNumber(qq)){
			addFieldError("errormessage","qq只能是数字");
			return;
		}
		
		UserOS userOS = new UserOS();
		
		try {
			
			if(!userOS.modifyPassowrdByCode(code,qq, password)){
				addFieldError("errormessage","修改密码失败,连接失效请重新找回密码连接");
				return;
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		userOS.releaseConnection();
		
	}
	
	public String getValicode() {
		return valicode;
	}
	public void setValicode(String valicode) {
		this.valicode = valicode;
	}
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		this.session = arg0;
	}
}
